package com.twentytwok.hospitalsystem22K.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.twentytwok.hospitalsystem22K.bo.DoctorBo;
import com.twentytwok.hospitalsystem22K.common.Code;
import com.twentytwok.hospitalsystem22K.common.PageVo;
import com.twentytwok.hospitalsystem22K.common.R;
import com.twentytwok.hospitalsystem22K.common.R2;
import com.twentytwok.hospitalsystem22K.mapper.DoctorUserMapper;
import com.twentytwok.hospitalsystem22K.pojo.DoctorUser;
import com.twentytwok.hospitalsystem22K.service.BookService;
import com.twentytwok.hospitalsystem22K.service.DoctorUserService;
import com.twentytwok.hospitalsystem22K.vo.DoctorVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 李腾达
 */

@Service("doctorUserService")
public class DoctorUserServiceImpl implements DoctorUserService {

    @Autowired
    private DoctorUserMapper doctorUserMapper;

    @Resource
    private BookService bookService;

    @Override
    public PageVo<DoctorUser> getPageData(Map<String, Object> params) {
        PageHelper.startPage(Integer.parseInt(params.get("page")+""),Integer.parseInt(params.get("limit")+""));

        List<DoctorUser> doctorUsers = doctorUserMapper.selectByParams(params);

        PageInfo<DoctorUser> doctorUserPageInfo = new PageInfo<>(doctorUsers);

        PageVo<DoctorUser> doctorUserPageVo = new PageVo<>();

        doctorUserPageVo.setCode(0);
        doctorUserPageVo.setMsg("查询用户分页数据");
        doctorUserPageVo.setCount(doctorUserPageInfo.getTotal());
        doctorUserPageVo.setData(doctorUserPageInfo.getList());
        return doctorUserPageVo;
    }

    @Override
    public R2 update(DoctorUser doctorUser) {
        int result = doctorUserMapper.update(doctorUser);

        if (result == 1){
            return R2.ok(200, "添加成功");
        } else {
            return R2.error(400, "添加失败");
        }
    }

    @Override
    public R2 delete(Integer id) {
        int result = doctorUserMapper.delete(id);

        if (result == 1){
            return R2.ok(200, "删除成功");
        } else {
            return R2.error(400, "删除失败");
        }
    }

    @Override
    public R2 add(DoctorUser doctorUser) {
        int result  = doctorUserMapper.add(doctorUser);

        if (result == 1){
            return R2.ok(200, "添加成功");
        } else {
            return R2.error(400, "添加失败");
        }
    }

    @Override
    public R getDoctorByDepartmentIdAndTimeQuantumId(Integer departmentId, LocalDate date, Integer timeQuantumId) {
        List<DoctorUser> doctorUsers = doctorUserMapper.selectByDepartmentId(departmentId);

        ///TODO 此处可能出现并发问题导致实际一个时间段的预约数量大于5的问题
        List<DoctorUser> newDoctorUsers = new ArrayList<>();  //用来存放合格的数据

        for (DoctorUser doctorUser : doctorUsers) {
            Integer count = bookService.getTodayBookCountByTimeQuantumId(doctorUser.getId(), date, timeQuantumId);

            if (count <= 5) {
                //将小于5的医生添加到新数组
                newDoctorUsers.add(doctorUser);
            }
        }

        //返回
        return new R(Code.OK, "ok", newDoctorUsers.size(), newDoctorUsers);
    }

    @Override
    public List<DoctorBo> selectDepartmentBo() {
        return doctorUserMapper.selectDepartment();
    }

    @Override
    public DoctorVo selectDepartmentVo() {
        List<DoctorBo> barBos = selectDepartmentBo();

        DoctorVo barVo = new DoctorVo();
        ArrayList<String> strings = new ArrayList<>();
        ArrayList<Integer> integers = new ArrayList<>();

        barVo.setNameList(strings);
        barVo.setNumList(integers);

        barBos.stream().forEach((bo) -> {
            String name = bo.getName();
            barVo.getNameList().add(name);
            Integer num = bo.getValue();

            barVo.getNumList().add(num);
        });

        return barVo;
    }

}
